using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common.DataSets;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.DataAccess
{
    /// <summary>
    /// User data access methods
    /// </summary>
    public class AspnetUser
    {
        #region Get/search methods

        /// <summary>
        /// Loads the details of a specific user based on user ID
        /// </summary>
        /// <param name="userID">The ID of the user to retrieve</param>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetUser GetByUserID(Guid userID)
        {
            dsAspnetUser dataSet = new dsAspnetUser();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            
            dataSet.Load(database.ExecuteReader("spAspnetUserSelectByID", userID), LoadOption.OverwriteChanges, "aspnet_Users");
            return dataSet;
        }

        /// <summary>
        /// Loads the details of a specific user based on user name
        /// </summary>
        /// <param name="userName">The user name of the user to retrieve</param>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetUser GetByUserName(string userName)
        {
            dsAspnetUser dataSet = new dsAspnetUser();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            dataSet.Load(database.ExecuteReader("spAspnetUserSelectByUserName", userName), LoadOption.OverwriteChanges, "aspnet_Users");
            return dataSet;
        }

        /// <summary>
        /// Loads the details of all users
        /// </summary>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetUser GetAll()
        {
            dsAspnetUser dataSet = new dsAspnetUser();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            dataSet.Load(database.ExecuteReader("spAspnetUserSelectAll"), LoadOption.OverwriteChanges, "aspnet_Users");
            return dataSet;
        }

        /// <summary>
        /// Returns the ID of an application based on its name (used for creating a new user)
        /// </summary>
        /// <param name="applicationName">The application name</param>
        /// <returns>The ID of the application</returns>
        public static Guid GetApplicationID(string applicationName)
        {
            // TODO: Create seperate AspnetApplication classes and move this to there
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return new Guid(database.ExecuteScalar("spAspnetApplicationGetIDByName", applicationName).ToString());
        }

        #endregion

        #region Insert and update methods

        /// <summary>
        /// Updates the details of a single user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int Update(AspnetMembershipUser user)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return database.ExecuteNonQuery("spAspnetUserUpdate", user.ApplicationID, user.UserID, user.UserName, 
                user.LoweredUserName, user.MobileAlias, user.IsAnonymous, user.LastActivityDate,
                user.HCUID, user.ProviderId);
        }
        
        /// <summary>
        /// Inserts a user into the database
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static Guid Insert(AspnetMembershipUser user)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return new Guid(database.ExecuteScalar("spAspnetUserInsert", user.ApplicationID, user.UserID, user.UserName, 
                user.LoweredUserName, user.MobileAlias, user.IsAnonymous, user.LastActivityDate,
                user.HCUID, user.ProviderId).ToString());
        }

        #endregion

        #region Delete methods

        /// <summary>
        /// Deletes a single user
        /// </summary>
        /// <param name="userID">The ID of the user</param>
        /// <returns>Returns the number of rows affected</returns>
        public static int Delete(Guid userID)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return database.ExecuteNonQuery("spAspnetUserDelete", userID);
        }

        #endregion


    }
}

